package tw.edu.tku.im.maze.database;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;

import tw.edu.tku.im.maze.AddRoomRep;
import tw.edu.tku.im.maze.AddRoomReq;
import tw.edu.tku.im.maze.Packet;

public class AddRoomProcessor extends PacketProcessor {

   @Override
   String acceptName() {
      return "addRoom";
   }

   @Override
   Packet process(Packet packet) {
      AddRoomReq req = (AddRoomReq)packet;
      Connection con;
      try {
         con = DriverManager.getConnection(address,user,passwd);
         con.setCatalog(database);
         CallableStatement cstmt = 
               con.prepareCall("{ call usp_addRoom(?, ?, ?, ?, ?, ?, ?) }");
//         cstmt.registerOutParameter(1, java.sql.Types.INTEGER);
//         cstmt.setString(1, req.getHomeowner());
//         cstmt.setInt(2, req.getTicket());
//         cstmt.execute();
//         int ticketCheck = cstmt.getInt(1);
         cstmt.setString(1, req.getRoom());
         cstmt.setString(2, req.getHomeowner());
         cstmt.setInt(3, req.getTicket());
         cstmt.setInt(4, req.getMapLv());
         cstmt.setInt(5, req.getMode());
         cstmt.setString(6, req.getRoomPassword());
         cstmt.registerOutParameter(7, Types.INTEGER);
//         cstmt.setNull(7, java.sql.Types.INTEGER);
//         cstmt.setNull(8, java.sql.Types.INTEGER);
//         cstmt.registerOutParameter(9, java.sql.Types.INTEGER);
         cstmt.execute();
         int roomID = cstmt.getInt(7);
         return new AddRoomRep(0, roomID);
      } catch (SQLException e) {
         e.printStackTrace();
      }
      return new AddRoomRep(1);
   }

}
